package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatTeamCategory;
import com.happy.cat.project.system.domain.CatTeamCategoryItem;
import com.happy.cat.project.system.service.ICatTeamCategoryItemService;
import com.happy.cat.project.system.service.ICatTeamCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 组队分类Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/teamCategory" )
@Api("组队分类 API")
public class CatTeamCategoryController extends BaseController {

    private final ICatTeamCategoryService iCatTeamCategoryService;

    private final ICatTeamCategoryItemService iCatTeamCategoryItemService;

    @GetMapping(value = "/all" )
    public AjaxResult all() {
        return AjaxResult.success(iCatTeamCategoryService.list());
    }
    /**
     * 查询组队分类列表
     */
    @ApiOperation(value = "查询组队分类列表",notes = "查询组队分类列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatTeamCategory catTeamCategory)
    {
        startPage();
        LambdaQueryWrapper<CatTeamCategory> lqw = new LambdaQueryWrapper<CatTeamCategory>();
        if (StringUtils.isNotBlank(catTeamCategory.getName())){
            lqw.like(CatTeamCategory::getName ,catTeamCategory.getName());
        }
        if (StringUtils.isNotBlank(catTeamCategory.getIcon())){
            lqw.eq(CatTeamCategory::getIcon ,catTeamCategory.getIcon());
        }
        if (catTeamCategory.getCreateDate() != null){
            lqw.eq(CatTeamCategory::getCreateDate ,catTeamCategory.getCreateDate());
        }
        if (catTeamCategory.getStatus() != null){
            lqw.eq(CatTeamCategory::getStatus ,catTeamCategory.getStatus());
        }
        List<CatTeamCategory> list = iCatTeamCategoryService.list(lqw);
        if(list.size()<=0){
            return getDataTable(list);
        }
        List<Long> ids = list.stream().map(CatTeamCategory::getId).collect(Collectors.toList());
        List<CatTeamCategoryItem> items = iCatTeamCategoryItemService.lambdaQuery().in(CatTeamCategoryItem::getTeamCategoryId, ids).list();
        Map<Long, List<CatTeamCategoryItem>> itemMap = items.stream().collect(Collectors.groupingBy(CatTeamCategoryItem::getTeamCategoryId));
        list.forEach(e ->{
            if(itemMap.get(e.getId())!=null){
                e.setItems(itemMap.get(e.getId()).size());
            }
        });

        return getDataTable(list);
    }

    /**
     * 导出组队分类列表
     */
    @ApiOperation(value = "导出组队分类列表",notes = "导出组队分类列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategory:export')" )
    @Log(title = "组队分类" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatTeamCategory catTeamCategory) {
        LambdaQueryWrapper<CatTeamCategory> lqw = new LambdaQueryWrapper<CatTeamCategory>(catTeamCategory);
        List<CatTeamCategory> list = iCatTeamCategoryService.list(lqw);
        ExcelUtil<CatTeamCategory> util = new ExcelUtil<CatTeamCategory>(CatTeamCategory. class);
        return util.exportExcel(list, "teamCategory" );
    }

    /**
     * 获取组队分类详细信息
     */
    @ApiOperation(value = "获取组队分类详细信息",notes = "获取组队分类详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategory:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatTeamCategoryService.getById(id));
    }

    /**
     * 新增组队分类
     */
    @ApiOperation(value = "新增组队分类",notes = "新增组队分类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategory:add')" )
    @Log(title = "组队分类" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatTeamCategory catTeamCategory) {
        catTeamCategory.setCreateDate(new Date());
        catTeamCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatTeamCategoryService.save(catTeamCategory) ? 1 : 0);
    }

    /**
     * 修改组队分类
     */
    @ApiOperation(value = "修改组队分类",notes = "修改组队分类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategory:edit')" )
    @Log(title = "组队分类" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatTeamCategory catTeamCategory) {
        catTeamCategory.setCreateDate(new Date());
        catTeamCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatTeamCategoryService.updateById(catTeamCategory) ? 1 : 0);
    }

    /**
     * 删除组队分类
     */
    @ApiOperation(value = "删除组队分类",notes = "删除组队分类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategory:remove')" )
    @Log(title = "组队分类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatTeamCategoryService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
